package cn.mesmile.flow.modules.processdefinition.service.impl;

import cn.mesmile.flow.modules.processdefinition.model.dto.ProcessDeployDTO;
import cn.mesmile.flow.modules.processdefinition.service.ProcessDefinitionService;
import lombok.AllArgsConstructor;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.IdentityService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.impl.util.Flowable5Util;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.Model;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.flowable.identitylink.api.IdentityLink;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;

/**
 * @author zb
 * @date 2020/12/7 12:58
 * @Description
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
@AllArgsConstructor
@Service
public class ProcessDefinitionServiceImpl implements ProcessDefinitionService {

    private final RepositoryService repositoryService;

    private final IdentityService identityService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deploy(ProcessDeployDTO processDeployDTO) {
//        Deployment deploy = repositoryService.createDeployment()
//                .key("")
//                .addString("xxxx.bpmn", "text")
//                .deploy();

        List<Model> list = repositoryService.createModelQuery().deployed().list();
        BpmnModel bpmnModel = repositoryService.getBpmnModel("");


        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("holiday-request.bpmn20.xml")
                .category("testtwo")
                .enableDuplicateFiltering()
                .name("请假任务流程")
                .tenantId("000000")
                .deploy();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId())
                .singleResult();

        String category = processDefinition.getCategory();
        String derivedFrom = processDefinition.getDerivedFrom();
        int version = processDefinition.getVersion();
        int derivedVersion = processDefinition.getDerivedVersion();
        String engineVersion = processDefinition.getEngineVersion();
        String id = processDefinition.getId();
        String key = processDefinition.getKey();
        String tenantId = processDefinition.getTenantId();
        String description = processDefinition.getDescription();
        String diagramResourceName = processDefinition.getDiagramResourceName();
        String name = processDefinition.getName();

        System.out.println(">>>>>>>>>>>>>>>>>>> 流程定义名称："+ processDefinition.getName());
        System.out.println(">>>>>>>>>>>>>>>>>>> 流程定义id："+ processDefinition.getId());

    }

    @Override
    public void get() {
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery()
                .latestVersion().list();
        processDefinitionList.forEach(processDefinition -> {
            String id = processDefinition.getId();
            System.out.println("id = " + id);
        });
        ProcessDefinition processDefinition1 = processDefinitionList.get(0);
        String processDefinitionId2 = processDefinition1.getId();


        /*
         <process id="potentialStarter" flowable:candidateStarterUsers="user1, user2"
                                        flowable:candidateStarterGroups="group1">
         */
        // 获取由指定用户发起的流程定义列表
        List<ProcessDefinition> processDefinitions =
                repositoryService.createProcessDefinitionQuery()
                .startableByUser("userId").list();

        // 也可查看流程定义中有哪些人可以启动
        String processDefinitionId = "financialReport:2:91e96017-39fa-11eb-921d-b2359f192e2c";

        List<IdentityLink> identityLinks = repositoryService
                .getIdentityLinksForProcessDefinition(processDefinitionId);
        for (IdentityLink identityLink : identityLinks) {
            String userId = identityLink.getUserId();
            String groupId = identityLink.getGroupId();
            String taskId = identityLink.getTaskId();
        }

        /**
         *  查询所有的流程定义列表 【所有的流程定义列表】
         *
         */
        List<ProcessDefinition> list =
                repositoryService.createProcessDefinitionQuery().active().latestVersion().list();
        ProcessDefinition processDefinition = list.get(0);
        String key = processDefinition.getKey();
        System.out.println(">>>>>>>>>>>> key:  "+ key);
        System.out.println(">>>>>>>>>>>>> ");



    }
}
